'use strict';
var utils=require('./utils');
var bizService = exports = module.exports = {};

bizService.initBanner = function(key){
	var banners =global.emoticonCache.banners
	var list=[]
	for(var i=0;i<banners.length;i++){
		var posMap = banners[i]
		var num = posMap.list.length
		var ind = Math.floor(Math.random()*num);
		list.push(posMap.list[ind])
		
	}
	
	return  list
}

bizService.initEmoFeed = function(key){
	var emoFeed =global.emoticonCache.emoFeed
	var list=[];
	for(var i=0;i<emoFeed.length;i++){
		var item = emoFeed[i]
		var feed={}
		feed.title=item.title
		feed.subTitle=item.subTitle;
		feed.icon=item.icon;
		feed.clickUrl=item.clickUrl;
		feed.items=[]
		
		var rary = utils.randomAry(item.items.length,8)

		for(var j=0;j<rary.length;j++){
			var it = item.items[rary[j]]
			var emo = {};
			emo.id=it.id;
			emo.title=it.ititle;
			emo.gif=it.gif;
			emo.thumb=it.thumb;
			emo.fileName=it.fileName;
			feed.items.push(emo)
			
		}
		list.push(feed)
		
	}
	
	return list
	
}

bizService.initEmoCat = function(key,catId){
	if(!catId )return null;
	var cat = global.emoticonCache.emoCatsMap[catId];
	var data ={}	
	if(cat){
		var list =[];
		for(var j=0;j<cat.items.length;j++){
			var scat = cat.items[j] 
			if(scat.items){
				var rary = utils.randomAry(scat.items.length,8)
				var obj={}
				obj.title=scat.title
 				obj.id=scat.id
 				obj.items=[];
 				for(var i=0;i<rary.length;i++){
 					obj.items.push(scat.items[rary[i]])
 				}
				list.push( obj)
			}
			
		}
		data.feed=list
		
			
	}
	return  utils.desEncrypt(JSON.stringify(data),key)
}

bizService.listByCat=function(key,catId,req){
	if(!catId)return null;
	var list = global.emoticonCache.subCats[catId];
	if(!list)return null;
	var page = req.query.page;
	if(!page) page =0
	var ps = global.config.emoPageSize
	var begin = page * ps
	var dlist = []
	if(begin < list.length){
		var slist= list.slice(begin,begin+ps)
		for(var i=0;i<slist.length;i++){
			var emo = {}
			var emoticon = slist[i]
			emo.id=emoticon.id
			emo.gif=emoticon.gif
			emo.thumb= global.config.imgPath + emoticon.thumb
			emo.fileName= global.config.imgPath + emoticon.fileName;
			dlist.push(emo)
		}
	}
	
	var pageData={}
	
	pageData.list=dlist
	pageData.page =  page
	pageData.num = dlist.length
	
	return  utils.desEncrypt(JSON.stringify(pageData),key)
}



bizService.initDictumFeed = function(key){
	var dictumFeed =global.emoticonCache.dictumFeed
	var list=[];
	for(var i=0;i<dictumFeed.length;i++){
		var item = dictumFeed[i]
		var feed={}
		feed.title=item.title
		feed.subTitle=item.subTitle;
		feed.icon=item.icon;
		feed.clickUrl=item.clickUrl;
		feed.items=[]
		
		var rary = utils.randomAry(item.items.length,2)
		
		for(var j=0;j<rary.length;j++){
			var it = item.items[rary[j]]
			var dictum = {};
			dictum.id=it.id;
			dictum.name=it.name;
			dictum.content=it.content;
			dictum.author=it.author;
			dictum.dynasty=it.dynasty;
			feed.items.push(dictum)
			
		}
		list.push(feed)
		
	}
	
	return list
	
}

bizService.initDicCat = function(key,catId){
	if(!catId )return null;
	var cat = global.emoticonCache.dictumCatsMap[catId+''];
	var data ={}	
	if(cat){
		
		var list =[];
		
		for(var j=0;j<cat.items.length;j++){
			var scat = cat.items[j] 
			if(scat.items){
				var rary = utils.randomAry(scat.items.length,2)
				var obj={}
				obj.title=scat.title
 				obj.id=scat.id
 				obj.items=[];
 				for(var i=0;i<rary.length;i++){
 					obj.items.push(scat.items[rary[i]])
 				}
				list.push( obj)
			}
			
		}
		data.feed=list	
	}
	return  utils.desEncrypt(JSON.stringify(data),key)
}

bizService.listDicByCat=function(key,catId,req){
	if(!catId)return null;
	var list = global.emoticonCache.subDicCats[catId];
	if(!list)return null;
	var page = req.query.page;
	if(!page) page =0
	var ps = global.config.dicPageSize
	var begin = page * ps
	var dlist = []
	if(begin < list.length){
		var slist= list.slice(begin,begin+ps)
		for(var i=0;i<slist.length;i++){
			var dictum = {}
			var it = slist[i]
			dictum.id=it.id;
	   				dictum.name=it.name;
	   				dictum.content=it.content;
	   				dictum.author=it.author;
	   				dictum.dynasty=it.dynasty;
			dlist.push(dictum)
		}
	}
	
	var pageData={}
	
	pageData.list=dlist
	pageData.page =  page
	pageData.num = dlist.length
	
	return  utils.desEncrypt(JSON.stringify(pageData),key)
}